package com.itheima.a12;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: AirMan
 * @date: 2025/4/11 20:59
 * @description: 模拟 jdk 动态代理
 */
public class jdkAOP {

    interface Foo{
        void foo();
        void bar();
    }

    interface InvocationHandler{
        void invoke(Method method, Object[] args) throws InvocationTargetException, IllegalAccessException;
    }

    static class Target implements Foo{
        @Override
        public void foo() {
            System.out.println("target foo, 原方法");
        }

        @Override
        public void bar() {
            System.out.println("target bar, 原方法");
        }
    }

    public static void main(String[] args) throws IOException {
        $MyJdkNewProxyInstance proxy = new $MyJdkNewProxyInstance(
                new InvocationHandler() {
                    @Override
                    public void invoke(Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
                        // 1.功能增强
                        System.out.println("before...");
                        // 2.调用目标(利用目标对象进行调用)
                        method.invoke(new Target(), args);
                        System.out.println("after...");
                    }
                }
        );

        // 调用代理方法
        proxy.foo();
        proxy.bar();

        System.out.println(proxy.getClass());

        System.in.read();
    }
}
